﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class PooledResource : IDisposable
{
    static private List<PooledResource> freeResourcesPool = new List<PooledResource>();

    private PooledResource()
    {
    }

    public PooledResource GetInstance()
    {
        if (freeResourcesPool.Count == 0)
        {
            PooledResource newResource = new PooledResource();
            freeResourcesPool.Add(newResource);
        }

        int index = freeResourcesPool.Count - 1;
        PooledResource r = freeResourcesPool[index];
        freeResourcesPool.RemoveAt(index);

        return r;
        
    }

    public void Release()
    {
        int index = freeResourcesPool.Count ;
        freeResourcesPool[index] = this;
    }

    ~PooledResource()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.Release();
        }
    }


        



}

